#pragma once
#include<assert.h>
#include<iostream>
using namespace std;

namespace wzy
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector(){}

		vector(const vector<T>& v)
		{
			reserve(v.size());
			for (auto e : v)
			{
				push_back(e);
			}
		}

		void swap(vector<T>& v)
		{
			std::swap(_star, v._star);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		vector<T>& operator=(const vector<T> v)
		{
			swap(v);
			return *this;
		}

		~vector()
		{
			delete[] _star;
			_star = _finish = _end_of_storage = nullptr;
		}

		bool empty()
		{
			return _star == _finish;
		}

		size_t size() const
		{
			return _finish - _star;
		}

		size_t capacity() const
		{
			return _end_of_storage - _star;
		}
		
		void resize(size_t n,const T& val=T())
		{
			if (n < size())
			{
				_finish = _star + n;
			}
			else
			{
				reserve(n);
				while (_finish < _star + n)
				{
					*_finish = val;
					++_finish;
				}

			}
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t old_size = size();
				T* tmp = new T[n];
				//memcpy(tmp, _star, sizeof(T) * size());
				for (size_t i = 0; i < old_size; ++i;)
				{
					tmp[i] = _str[i];
				}
				delete[] _star;
				_star = tmp;
				_finish = tmp + old_size;
				_end_of_storage = tmp + n;
			}
		}

		void push_back(const T& x)
		{
			if (_finish == _end_of_storage)
			{
				reserve(_end_of_storage == 0 ? 4 : 2 * capacity()); 
			}
			*_finish = x;
			++_finish;
		}
		void pop_back()
		{
			assert(!empty());
			--_finish;
		}
		iterator& insert(iterator pos,const T& x)
		{
			if (_finish == _end_of_storage)
			{
				size_t len = pos - _star;
				reserve(_end_of_storage == 0 ? 4 : 2 * capacity());
				pos = _star + len;
			}
			iterator end = _finish;
			while (end > pos)
			{
				*(end) = *(end - 1);
				--end;
			}
			*pos = x;
			++_finish;
			return pos;
		}

		void earse(iterator pos)
		{
			iterator it = pos + 1;
			while (it != end())
			{
				*(it - 1) = *it;
				++it;
			}
			--_finish;
		}

		T& operator[] (size_t i)
		{
			return _star[i];
		}

		const T& operator[] (size_t i) const
		{
			return _star[i];
		}
		
		iterator begin()
		{
			return _star;
		}
		iterator end()
		{
			return _finish;
		}
		const_iterator begin()const
		{
			return _star;
		}
		const_iterator end()const
		{
			return _finish;
		}
	private:
		iterator _star = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};
	void test_vector1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		v.push_back(5);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}
	void test_vector2()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		int x;
		cin >> x;
		vector<int>::iterator it = find(v.begin(), v.end(), x);
		if (it != v.end())
		{
			it = v.insert(it, 20);

			*(it+1) *= 10;
		}
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}
}